<!--
 * @description 组件
 * @author: 陈陈
 * @date: 2025/05/09
 * @phone: 18560000860
 * @email: 18560000860@qq.com
 * @company: 济南晨霜信息技术有限公司 /
 * @business: 承接前后端项目的开发
-->
<template>
	<view
		class="pure-avatar"
		:class="_classes"
		:style="_styles"
		:hover-class="hoverClass"
		@tap="_handleClick"
	>
		<image
			class="pure-avatar__image"
			:src="src"
			:mode="mode"
			@load="_handleLoad"
			@error="_handleError"
		/>
		<image
			class="pure-avatar__default"
			:src="defaultSrc"
			:mode="mode"
			v-if="defaultSrc && status !== 'success'"
		/>
		<view
			class="pure-avatar__edit"
			v-if="showEdit"
		>
			<pure-icon :name="editIconName"></pure-icon>
		</view>
		<view class="pure-avatar__body">
			<slot></slot>
		</view>
	</view>
</template>

<script>
	import commonComponentOptions from "../../config/options";
	export default {
		options: commonComponentOptions
	};
</script>

<script setup>
	import { ref, computed } from "vue";
	import props from "./props";

	// 加载状态
	const status = ref("loading");

	// -------------------------------------------------------------------------------- Props
	const _props = defineProps(props);

	// -------------------------------------------------------------------------------- Emits
	const _emits = defineEmits(["onClick", "onLoad", "onError"]);

	// -------------------------------------------------------------------------------- Classes
	const _classes = computed(() => {
		const array = [];
		return array;
	});

	// -------------------------------------------------------------------------------- Styles
	const _styles = computed(() => {
		const array = [];

		// 大小
		if (_props.size) array.push(`--pure-avatar-size: ${_props.size}`);

		return array;
	});

	// 加载成功
	function _handleLoad(e) {
		_emits("onLoad", e);
		status.value = "success";
	}

	// 加载失败
	function _handleError(e) {
		_emits("onError", e);
		status.value = "error";
	}

	// 点击事件
	function _handleClick() {
		if (_props.preview) {
			uni.previewImage({
				urls: [_props.src],
				current: _props.src
			});
		}
		_emits("onClick");
	}
</script>

<style scoped lang="scss">
	@import "./style.scss";
</style>
